from flask_login import logout_user, current_user
from forms.login_form import LoginForm
from models.gep import Gep, GepTest
from routes import app
from flask import render_template, abort, redirect, url_for, flash
from services.user_service import UserService
from flask import Flask, request, jsonify


@app.route('/gep', methods=['GET'])
def gep_data():
    gepCity = request.args.get('gepCity', '商洛市')
    gepDistrict = request.args.get('gepDistrict', '总计')
    gepTown = request.args.get('gepTown', '总计')
    year = request.args.get('gepYear', 2023, type=int)

    # 查询数据库
    result = Gep.query.filter_by(gepCity=gepCity, gepDistrict=gepDistrict, gepTown=gepTown, year=year).first()
    print('gep-result',result.outWaterSourceConservation)
    if result:
        # 返回符合要求的数据格式
        data = {
            'gepCity':result.gepCity,
            'gepDistrict': result.gepDistrict,
            'gepTown': result.gepTown,
            'outWaterSourceConservation': result.outWaterSourceConservation,
            'outSoilConservation': result.outSoilConservation,
            'outNegativeOxygenIons': result.outNegativeOxygenIons,
            'outAirPurification': result.outAirPurification,
            'outWaterPurification': result.outWaterPurification,
            'outCarbonSequestration': result.outCarbonSequestration,
            'outOxygenRelease': result.outOxygenRelease,
            'outClimateRegulation': result.outClimateRegulation,
            'outCalcFloodRegulation': result.outCalcFloodRegulation,
            'outCalcBiodiversityProtection': result.outCalcBiodiversityProtection,
            'outCalcNoiseReduced': result.outCalcNoiseReduced
        }
        print('gep-data', data)
        return jsonify(data)
    else:
        return jsonify({'error': 'Data not found'}), 404


@app.route('/gepTest', methods=['GET'])
def gep_test():
    gepCity = request.args.get('gepCity', '商洛市')
    gepDistrict = request.args.get('gepDistrict', '总计')
    gepTown = request.args.get('gepTown', '总计')
    year = request.args.get('gepYear', 2023, type=int)

    # 查询数据库
    result = GepTest.query.filter_by(gepCity=gepCity, gepDistrict=gepDistrict, gepTown=gepTown, year=year).first()
    print('gepTest-result',result.inAgricultural)
    if result:
        # 返回符合要求的数据格式
        data = {
            'gepCity': result.gepCity,
            'gepDistrict': result.gepDistrict,
            'gepTown': result.gepTown,
            'inAgricultural': result.inAgricultural,
            'inAnimalHusbandry': result.inAnimalHusbandry,
            'inForestry': result.inForestry,
            'inFishery': result.inFishery,
            'inEcologicalEnergy': result.inEcologicalEnergy,
            'inWaterResources': result.inWaterResources,
            'inLocalWaterPrice': result.inLocalWaterPrice,
            'inProtectedArea': result.inProtectedArea,
            'inProtectedCost': result.inProtectedCost,
            'inReservoirCapacity': result.inReservoirCapacity,
            'inReservoirCost': result.inReservoirCost,
            'inHighwayMileage': result.inHighwayMileage,
            'inHighwayGreeningDegree': result.inHighwayGreeningDegree,
            'inTourismRevenue': result.inTourismRevenue,
            'inDisposableIncome': result.inDisposableIncome,
            'inGreenSpacePerCapita': result.inGreenSpacePerCapita,
            'inUrbanPopulation': result.inUrbanPopulation,
            'inHotelRooms': result.inHotelRooms,
            'inRoomPrice': result.inRoomPrice,
            'inHousingAreaPerCapita': result.inHousingAreaPerCapita
        }
        print('gepTest-data', data)
        return jsonify(data)
    else:
        return jsonify({'error': 'TestData not found'}), 404
